package com.guigu.servicesystem.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.common_util.pojo.utils.Result;
import com.guigu.servicesystem.entity.TbAdmin;
import com.guigu.servicesystem.entity.TbAdminRole;
import com.guigu.servicesystem.entity.TbRole;
import com.guigu.servicesystem.entity.vo.Admin_role;
import com.guigu.servicesystem.service.TbAdminRoleService;
import com.guigu.servicesystem.service.TbAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@RestController
@RequestMapping("/servicesystem/admin")
public class TbAdminController {

    @Autowired
    private TbAdminService tbAdminService;
    @Autowired
    private TbAdminRoleService tbAdminRoleService;
    //查询所有 0
    @GetMapping("findAll")
    public Result findAllTbAdmin(){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("status","1");
        List<TbAdmin> list = tbAdminService.list(wrapper);
        return  Result.ok().data("items",list);
    }
    //逻辑删除0
    @DeleteMapping("delete")
    public Result removeTbAdmin(Integer id) {
        boolean flag = tbAdminService.removeById(id);
        System.out.println(flag);
        if(flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }
    //条件+分页查询数据 0
    @PostMapping("findPage")
    public Result findPage(@RequestBody Map<String, Object> searchMap, Integer page, Integer size) {
        return tbAdminService.findPage(searchMap, page, size);
    }
    //添加接口的方法0
    @PostMapping("add")
    public Result addTbAdmin(@RequestBody TbAdmin tbAdmin) {
        boolean save = tbAdminService.save(tbAdmin);
        if(save) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    //根据id查询 0
    @GetMapping("findById")
    public Result getTbAdmin(String id) {
        TbAdmin tbAdmin = tbAdminService.getById(id);
        return Result.ok().data("tbAdmin",tbAdmin);
    }
    //修改功能0
    @PostMapping("update")
    public Result updateTbAdmin(@RequestBody Admin_role addForm){
        tbAdminService.updataAdmin(addForm);
       return Result.ok();
    }
    //查询所有账户和对应的权限
    @GetMapping("find")
    public Result findAll(int status){
        List<Admin_role> admin_role= tbAdminService.selectAllAdmin_Role(status);
        return Result.ok().data("admin",admin_role);
    }
    //寻找单个账户
    @GetMapping("findByAdmin")
    public Result findByAdmin(int id){
        Admin_role admin_role = tbAdminService.findbyAdmin(id);
        return Result.ok().data("result",admin_role);
    }
    //添加账户，并且关联对应的角色
    @PostMapping("savaAdmim")
    public Result SavaAdmin(@RequestBody Admin_role addForm){
        tbAdminService.insert(addForm);
        return Result.ok();
    }
    @Transactional
    @DeleteMapping("deleteByID")
    public Result deletebyID(int id){
        tbAdminService.removeById(id);
        tbAdminRoleService.removeById(id);
        return Result.ok();
    }
    @Transactional
    @PostMapping("changstatus")
    public Result changestatus(@RequestBody Admin_role val){
        TbAdmin admin=new TbAdmin();
        admin.setId(val.getId());
        admin.setStatus(val.getStatus());
        admin.setLoginName(val.getLoginName());
        admin.setPassword(val.getPassword());
        tbAdminService.updateById(admin);
        return Result.ok();
    }
}

